﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using SuntabuAssistant.AssistAchieve;
using SuntabuAssistant.Voice2Redmine.Recognise;
using SuntabuAssistant.Voice2Redmine.wave;
using vtmblip.ext;

namespace SuntabuAssistant.Voice2Redmine.KeyControlRec
{
    public class VoiceControl : Singleton<VoiceControl>
    {
        protected Recorder Rec;
        private string file;
        private VoiceControl()
        {

        }

        public void VoiceRecord()
        {
            try
            {
                file = Path.GetTempFileName();

                Rec = new Recorder(file);
                System.Threading.Thread.Sleep(1);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }

        }

        public VoiceTextData VoiceRecogise()
        {

            Rec.Stop();
            while (!Rec.AllDone) { }
            GC.SuppressFinalize(Rec);

            var recogn = new GoogleRecognizer();

            GoogleResponse respdata = null;
            try
            {
                respdata = recogn.RecognizeFlac(file);
            }
            catch (Exception e)
            {
                respdata = null;
                Debug.WriteLine(e.Message);
            }
            if (respdata == null)
                return null;

            File.Delete(file);
            //Json
            return new VoiceTextData() { Rating = respdata.hypotheses[0].confidence, Recognised = respdata.hypotheses[0].utterance };
        }
    }
}
